package com.zp.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.paper.ResultPaperVo;
import com.zp.domain.Paper;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * paper services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/22
 * 
 */
@Component
@Path("/rest/paper")
@Singleton
public class PaperServices extends BaseServices {

	/**
	 * add one Paper path:/paper/addPaper Http:POST
	 * 
	 * @param paper
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/addPaper")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addPaper(Paper paper) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(paper);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update one Paper path:/paper/updatePaper Http:PUT
	 * 
	 * @param paper
	 * 
	 * @return Response, ResultPaperVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0,"paper":{"id":
	 *         "402881e6445c883e01445c8a74290001","name":"心理关爱"},"list":null}
	 */
	@PUT
	@Path("/updatePaper")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updatePaper(Paper paper) {
		ResultPaperVo result = new ResultPaperVo();
		try {
			jpaDao.update(paper);
			result.setPaper(paper);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete Paper by id path:/paper/deletePaper Http:DELETE
	 * 
	 * @param paperId
	 *            primary key of Paper
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deletePaper")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deletePaper(@QueryParam("paperId") String paperId) {
		logger.info("deletePaper called paperId:" + paperId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(Paper.class, paperId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get Paper by id path:/paper/getPaper Http:GET
	 * 
	 * @param paperId
	 *            primary key of Paper
	 * 
	 * @return Response, ResultPaperVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0,"paper":{"id":
	 *         "402881e6445c883e01445c8a74290001","name":"心理关爱"},"list":null}
	 * 
	 */
	@GET
	@Path("/getPaper")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getPaperById(@QueryParam("paperId") String paperId) {
		logger.info("getPaperById called paperId:" + paperId);
		ResultPaperVo result = new ResultPaperVo();
		try {
			Paper paper = (Paper) jpaDao.findById(Paper.class, paperId);
			result.setPaper(paper);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * 
	 * get paper with page parameters and search condition path:/paper/getPapers
	 * Http:GET
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @param paperName
	 *            the name of paper,if ignored the value is null
	 * 
	 * @return Response, ResultPaperVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":1,"paper":null,"list"
	 *         :[{"id":"402881e6445c883e01445c8a74290001","name":"心理关爱"}]}
	 * 
	 * 
	 */
	@GET
	@Path("/getPapers")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getPaperByCodeName(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("paperName") String paperName) {
		logger.info("getPaperByCritera called json pageNo:" + pageNo
				+ " pageSize:" + pageSize + " paperName:" + paperName);
		String jpql = "from Paper where name=? order by name asc";
		String countSql = "select count(id) from Paper where name=? order by name asc";
		List<?> list = null;
		ResultPaperVo result = new ResultPaperVo();
		List<Paper> lt = new ArrayList<Paper>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(paperName) || paperName == null) {
			jpql = "from Paper order by name asc";
			countSql = "select count(id) from Paper order by name asc";
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, null);
				long totalCount = this.jpaDao.getCount(countSql, null);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		} else {
			Object[] param = { paperName };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, param);
				long totalCount = this.jpaDao.getCount(countSql, param);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Paper paper = (Paper) o;
				lt.add(paper);
			}
			result.setList(lt);

		}
		return Response.ok(result).build();
	}
}
